package com.rongshu.view.penpal.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.view.base.BaseFragment;
import com.rongshu.view.base.mvp.INetView;
import com.rongshu.view.penpal.adapter.PenpalListAdapter;
import com.rongshu.view.penpal.bean.PenpalResponse;
import com.rongshu.view.penpal.present.PenpalNearbyPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * created by zhengweis on 2018/11/5.
 * description：笔友会-附近
 */
public class Penpal_NearbyFragment extends BaseFragment<PenpalNearbyPresenter> implements INetView {
    @BindView(R.id.penpal_list)
    RecyclerView penpalList;
    @BindView(R.id.refresh_view)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.no_data_img)
    ImageView noDataImg;
    @BindView(R.id.hint_1)
    TextView hint1;
    @BindView(R.id.no_data_layout)
    RelativeLayout noDataLayout;

    private int pageIndex = 1;
    private int pageSize = 10;
    private String msg = "";
    private List<PenpalResponse.ResponseBean> list;
    private PenpalListAdapter adapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_penpal_nearby;
    }

    @Override
    protected void initViews() {
        refreshLayout.setEnableAutoLoadmore(false);
    }

    @Override
    protected void initData() {
        setPresenter(new PenpalNearbyPresenter(getActivity()), this);

        refreshLayout.setEnableAutoLoadmore(false);
        list = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        penpalList.setLayoutManager(linearLayoutManager);
        adapter = new PenpalListAdapter(getActivity(), list);
        penpalList.setAdapter(adapter);

        if (TextUtils.isEmpty(presenter.getUserCity())) {
            noDataLayout.setVisibility(View.VISIBLE);
            noDataImg.setImageResource(R.drawable.empty_address);
        } else {
            msg = presenter.getUserCity();
            noDataLayout.setVisibility(View.GONE);
            refreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(RefreshLayout refreshlayout) {
                    pageIndex = 1;
                    refreshlayout.resetNoMoreData();
                    presenter.searchPenpalList(msg, pageIndex, pageSize);
                    refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                }
            });
            refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
                @Override
                public void onLoadmore(RefreshLayout refreshlayout) {
                    pageIndex++;
                    presenter.searchPenpalList(msg, pageIndex, pageSize);
                    refreshlayout.finishLoadmore(2000/*,false*/);//传入false表示刷新失败
                }
            });
        }
        if (!TextUtils.isEmpty(presenter.getUserCity())) {
            pageIndex = 1;
            refreshLayout.resetNoMoreData();
            presenter.searchPenpalList(msg, pageIndex, pageSize);
        }
    }

    @Override
    protected void updateViews(boolean isRefresh) {
    }

    @Override
    public void success(Object object, int tag) {
        PenpalResponse response = (PenpalResponse) object;
        if (response != null && response.getResponse() != null) {
            if (pageIndex == 1) {
                list.clear();
            }
            if (refreshLayout.isRefreshing()) {
                refreshLayout.finishRefresh();
            }
            if (refreshLayout.isLoading()) {
                refreshLayout.finishLoadmore();
            }
            list.addAll(response.getResponse());
            adapter.notifyDataSetChanged();
            if (response.getResponse().size() == 0) {
                refreshLayout.setLoadmoreFinished(true);
            }
            if (list.size() == 0) {
                noDataLayout.setVisibility(View.VISIBLE);
                refreshLayout.setEnableLoadmore(false);
                refreshLayout.setEnableRefresh(false);
                noDataImg.setImageResource(R.drawable.empty_pen_pal_will_bg);
                hint1.setText("未发现附近笔友会");
            }
        }
    }

    @Override
    public void faild(Object object, int tag) {

    }
}
